"""单例装饰器"""


def singleton(cls):
    """实现一个单例装饰器"""
    _instance = {}
    def wrapper_singleton(*args, **kwargs):
        """
        缓存了所有单例类，只要单例不存在则创建，已经存在直接返回该实例对象。
        """
        if cls not in _instance:
            _instance[cls] = cls(*args, **kwargs)
        return _instance

    return wrapper_singleton


@singleton
class Test:
    """测试，直接返回pass"""
    pass


test1 = Test()
test2 = Test()
print(test1)
print(test2)

assert test1 is test2, "不是单例"
